package o;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.FragmentActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.manager.SupportRequestManagerFragment;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class yt implements Callback {
    private static final e ॱॱ = new e() {
        @NonNull
        public rp ॱ(@NonNull Glide glide, @NonNull yo yoVar, @NonNull yr yrVar, @NonNull Context context) {
            return new rp(glide, yoVar, yrVar, context);
        }
    };
    private final ds<View, Fragment> ʻ = new ds();
    private final Bundle ʽ = new Bundle();
    @VisibleForTesting
    final Map<FragmentManager, SupportRequestManagerFragment> ˊ = new HashMap();
    private volatile rp ˋ;
    private final e ˎ;
    @VisibleForTesting
    final Map<android.app.FragmentManager, yp> ˏ = new HashMap();
    private final Handler ॱ;
    private final ds<View, androidx.fragment.app.Fragment> ᐝ = new ds();

    public interface e {
        @NonNull
        rp ॱ(@NonNull Glide glide, @NonNull yo yoVar, @NonNull yr yrVar, @NonNull Context context);
    }

    public yt(@Nullable e eVar) {
        if (eVar == null) {
            eVar = ॱॱ;
        }
        this.ˎ = eVar;
        this.ॱ = new Handler(Looper.getMainLooper(), this);
    }

    @NonNull
    private rp ˋ(@NonNull Context context) {
        if (this.ˋ == null) {
            synchronized (this) {
                if (this.ˋ == null) {
                    this.ˋ = this.ˎ.ॱ(Glide.ˎ(context.getApplicationContext()), new yg(), new yn(), context.getApplicationContext());
                }
            }
        }
        return this.ˋ;
    }

    @NonNull
    public rp ॱ(@NonNull Context context) {
        if (context == null) {
            throw new IllegalArgumentException("You cannot start a load on a null Context");
        }
        if (aau.ˎ() && !(context instanceof Application)) {
            if (context instanceof FragmentActivity) {
                return ˋ((FragmentActivity) context);
            }
            if (context instanceof Activity) {
                return ˊ((Activity) context);
            }
            if ((context instanceof ContextWrapper) && ((ContextWrapper) context).getBaseContext().getApplicationContext() != null) {
                return ॱ(((ContextWrapper) context).getBaseContext());
            }
        }
        return ˋ(context);
    }

    @NonNull
    public rp ˋ(@NonNull FragmentActivity fragmentActivity) {
        if (aau.ˋ()) {
            return ॱ(fragmentActivity.getApplicationContext());
        }
        ˎ((Activity) fragmentActivity);
        return ˏ(fragmentActivity, fragmentActivity.ˏॱ(), null, ˊ((Context) fragmentActivity));
    }

    @NonNull
    public rp ˏ(@NonNull androidx.fragment.app.Fragment fragment) {
        aas.ˋ(fragment.ॱˊ(), "You cannot start a load on a fragment before it is attached or after it is destroyed");
        if (aau.ˋ()) {
            return ॱ(fragment.ॱˊ().getApplicationContext());
        }
        return ˏ(fragment.ॱˊ(), fragment.ʽॱ(), fragment, fragment.ˉ());
    }

    @NonNull
    public rp ˊ(@NonNull Activity activity) {
        if (aau.ˋ()) {
            return ॱ(activity.getApplicationContext());
        }
        ˎ(activity);
        return ˊ(activity, activity.getFragmentManager(), null, ˊ((Context) activity));
    }

    @NonNull
    public rp ˋ(@NonNull View view) {
        if (aau.ˋ()) {
            return ॱ(view.getContext().getApplicationContext());
        }
        aas.ˎ(view);
        aas.ˋ(view.getContext(), "Unable to obtain a request manager for a view without a Context");
        Activity ˎ = ˎ(view.getContext());
        if (ˎ == null) {
            return ॱ(view.getContext().getApplicationContext());
        }
        if (ˎ instanceof FragmentActivity) {
            androidx.fragment.app.Fragment ॱ = ॱ(view, (FragmentActivity) ˎ);
            return ॱ != null ? ˏ(ॱ) : ˋ((FragmentActivity) ˎ);
        } else {
            Fragment ˏ = ˏ(view, ˎ);
            if (ˏ == null) {
                return ˊ(ˎ);
            }
            return ॱ(ˏ);
        }
    }

    private static void ॱ(@Nullable Collection<androidx.fragment.app.Fragment> collection, @NonNull Map<View, androidx.fragment.app.Fragment> map) {
        if (collection != null) {
            for (androidx.fragment.app.Fragment fragment : collection) {
                if (!(fragment == null || fragment.getView() == null)) {
                    map.put(fragment.getView(), fragment);
                    ॱ(fragment.ʽॱ().ˏ(), (Map) map);
                }
            }
        }
    }

    @Nullable
    private androidx.fragment.app.Fragment ॱ(@NonNull View view, @NonNull FragmentActivity fragmentActivity) {
        this.ᐝ.clear();
        ॱ(fragmentActivity.ˏॱ().ˏ(), this.ᐝ);
        androidx.fragment.app.Fragment fragment = null;
        View findViewById = fragmentActivity.findViewById(16908290);
        while (!view.equals(findViewById)) {
            fragment = (androidx.fragment.app.Fragment) this.ᐝ.get(view);
            if (fragment != null || !(view.getParent() instanceof View)) {
                break;
            }
            view = (View) view.getParent();
        }
        this.ᐝ.clear();
        return fragment;
    }

    @Deprecated
    @Nullable
    private Fragment ˏ(@NonNull View view, @NonNull Activity activity) {
        this.ʻ.clear();
        ˏ(activity.getFragmentManager(), this.ʻ);
        Fragment fragment = null;
        View findViewById = activity.findViewById(16908290);
        while (!view.equals(findViewById)) {
            fragment = (Fragment) this.ʻ.get(view);
            if (fragment != null || !(view.getParent() instanceof View)) {
                break;
            }
            view = (View) view.getParent();
        }
        this.ʻ.clear();
        return fragment;
    }

    @TargetApi(26)
    @Deprecated
    private void ˏ(@NonNull android.app.FragmentManager fragmentManager, @NonNull ds<View, Fragment> dsVar) {
        if (VERSION.SDK_INT >= 26) {
            for (Fragment fragment : fragmentManager.getFragments()) {
                if (fragment.getView() != null) {
                    dsVar.put(fragment.getView(), fragment);
                    ˏ(fragment.getChildFragmentManager(), (ds) dsVar);
                }
            }
            return;
        }
        ˋ(fragmentManager, dsVar);
    }

    @Deprecated
    private void ˋ(@NonNull android.app.FragmentManager fragmentManager, @NonNull ds<View, Fragment> dsVar) {
        int i = 0;
        while (true) {
            int i2 = i + 1;
            this.ʽ.putInt("key", i);
            Fragment fragment = null;
            try {
                fragment = fragmentManager.getFragment(this.ʽ, "key");
            } catch (Exception e) {
            }
            if (fragment != null) {
                if (fragment.getView() != null) {
                    dsVar.put(fragment.getView(), fragment);
                    if (VERSION.SDK_INT >= 17) {
                        ˏ(fragment.getChildFragmentManager(), (ds) dsVar);
                    }
                }
                i = i2;
            } else {
                return;
            }
        }
    }

    @Nullable
    private static Activity ˎ(@NonNull Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        }
        if (context instanceof ContextWrapper) {
            return ˎ(((ContextWrapper) context).getBaseContext());
        }
        return null;
    }

    @TargetApi(17)
    private static void ˎ(@NonNull Activity activity) {
        if (VERSION.SDK_INT >= 17 && activity.isDestroyed()) {
            throw new IllegalArgumentException("You cannot start a load for a destroyed activity");
        }
    }

    @TargetApi(17)
    @NonNull
    @Deprecated
    public rp ॱ(@NonNull Fragment fragment) {
        if (fragment.getActivity() == null) {
            throw new IllegalArgumentException("You cannot start a load on a fragment before it is attached");
        } else if (aau.ˋ() || VERSION.SDK_INT < 17) {
            return ॱ(fragment.getActivity().getApplicationContext());
        } else {
            return ˊ(fragment.getActivity(), fragment.getChildFragmentManager(), fragment, fragment.isVisible());
        }
    }

    @NonNull
    @Deprecated
    yp ˋ(Activity activity) {
        return ˋ(activity.getFragmentManager(), null, ˊ((Context) activity));
    }

    @NonNull
    private yp ˋ(@NonNull android.app.FragmentManager fragmentManager, @Nullable Fragment fragment, boolean z) {
        yp ypVar = (yp) fragmentManager.findFragmentByTag("com.bumptech.glide.manager");
        if (ypVar == null) {
            ypVar = (yp) this.ˏ.get(fragmentManager);
            if (ypVar == null) {
                ypVar = new yp();
                ypVar.ˋ(fragment);
                if (z) {
                    ypVar.ˏ().ˎ();
                }
                this.ˏ.put(fragmentManager, ypVar);
                fragmentManager.beginTransaction().add(ypVar, "com.bumptech.glide.manager").commitAllowingStateLoss();
                this.ॱ.obtainMessage(1, fragmentManager).sendToTarget();
            }
        }
        return ypVar;
    }

    @NonNull
    @Deprecated
    private rp ˊ(@NonNull Context context, @NonNull android.app.FragmentManager fragmentManager, @Nullable Fragment fragment, boolean z) {
        yp ˋ = ˋ(fragmentManager, fragment, z);
        rp ˋ2 = ˋ.ˋ();
        if (ˋ2 != null) {
            return ˋ2;
        }
        ˋ2 = this.ˎ.ॱ(Glide.ˎ(context), ˋ.ˏ(), ˋ.ˊ(), context);
        ˋ.ˋ(ˋ2);
        return ˋ2;
    }

    @NonNull
    public SupportRequestManagerFragment ˊ(Context context, FragmentManager juVar) {
        return ˊ(juVar, null, ˊ(context));
    }

    private static boolean ˊ(Context context) {
        Activity ˎ = ˎ(context);
        return ˎ == null || !ˎ.isFinishing();
    }

    @NonNull
    private SupportRequestManagerFragment ˊ(@NonNull FragmentManager juVar, @Nullable androidx.fragment.app.Fragment fragment, boolean z) {
        SupportRequestManagerFragment supportRequestManagerFragment = (SupportRequestManagerFragment) juVar.findFragmentByTag("com.bumptech.glide.manager");
        if (supportRequestManagerFragment == null) {
            supportRequestManagerFragment = (SupportRequestManagerFragment) this.ˊ.get(juVar);
            if (supportRequestManagerFragment == null) {
                supportRequestManagerFragment = new SupportRequestManagerFragment();
                supportRequestManagerFragment.ˊ(fragment);
                if (z) {
                    supportRequestManagerFragment.ॱ().ˎ();
                }
                this.ˊ.put(juVar, supportRequestManagerFragment);
                juVar.ˊ().ˋ((androidx.fragment.app.Fragment) supportRequestManagerFragment, "com.bumptech.glide.manager").ˋ();
                this.ॱ.obtainMessage(2, juVar).sendToTarget();
            }
        }
        return supportRequestManagerFragment;
    }

    @NonNull
    private rp ˏ(@NonNull Context context, @NonNull FragmentManager juVar, @Nullable androidx.fragment.app.Fragment fragment, boolean z) {
        SupportRequestManagerFragment ˊ = ˊ(juVar, fragment, z);
        rp ʼᐝ = ˊ.ʼᐝ();
        if (ʼᐝ != null) {
            return ʼᐝ;
        }
        ʼᐝ = this.ˎ.ॱ(Glide.ˎ(context), ˊ.ॱ(), ˊ.ʾॱ(), context);
        ˊ.ˋ(ʼᐝ);
        return ʼᐝ;
    }

    public boolean handleMessage(Message message) {
        Object obj = null;
        boolean z = true;
        Object remove;
        switch (message.what) {
            case 1:
                android.app.FragmentManager fragmentManager = (android.app.FragmentManager) message.obj;
                remove = this.ˏ.remove(fragmentManager);
                break;
            case 2:
                FragmentManager juVar = (FragmentManager) message.obj;
                remove = this.ˊ.remove(juVar);
                break;
            default:
                z = false;
                remove = null;
                break;
        }
        if (z && r1 == null && Log.isLoggable("RMRetriever", 5)) {
            Log.w("RMRetriever", "Failed to remove expected request manager fragment, manager: " + obj);
        }
        return z;
    }
}
